package com.yuanchu.goods.controller;

import com.yuanchu.common.utils.PageUtils;
import com.yuanchu.common.utils.R;
import com.yuanchu.common.validator.ValidationGroups;
import com.yuanchu.goods.dto.GoodsServiceDto;
import com.yuanchu.goods.entity.YoshopGoodsServiceEntity;
import com.yuanchu.goods.service.YoshopGoodsServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 商品服务与承诺记录表
 *
 * @author adan
 * @email 2028038953@gmail.com
 * @date 2023-12-19 02:17:43
 */
@Api(tags = "商品服务与承诺记录接口", description = "商品服务与承诺记录接口")
@RestController
@RequestMapping("service")
public class YoshopGoodsServiceController {
    @Resource
    private YoshopGoodsServiceService yoshopGoodsServiceService;

    private int storeId = 10001;

    /**
     * 商品服务与承诺列表
     */
    @ApiOperation("查询服务与承诺列表")
    @GetMapping("/list")
    public R list(@RequestParam(required = false) Map<String, Object> params) {
        PageUtils pageUtils = yoshopGoodsServiceService.queryGoodsService(params);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", pageUtils);
        return R.ok().put("data", map);
    }

    @ApiOperation("根据商品id查询服务与承诺列表")
    @GetMapping("/store/list")
    public R queryGoodsServiceByGoods(@RequestParam Integer goodsId) {
        List<YoshopGoodsServiceEntity> serviceList = yoshopGoodsServiceService.queryGoodsServiceByGoods(goodsId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", serviceList);
        return R.ok().put("data", map);
    }

    @ApiOperation("查询服务与承诺列表")
    @GetMapping("/all")
    public R all() {
        List<YoshopGoodsServiceEntity> list = yoshopGoodsServiceService.queryGoodsAll();
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", list);
        return R.ok().put("data", map);
    }


    /**
     * 保存
     */
    @ApiOperation("添加服务与承诺")
    @RequestMapping("/add")
    public R add(@RequestBody @Validated(ValidationGroups.Insert.class) GoodsServiceDto form) {
        yoshopGoodsServiceService.addGoodsService(form, storeId);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改服务与承诺")
    @RequestMapping("/edit")
    public R edit(@RequestBody @Validated(ValidationGroups.Update.class) GoodsServiceDto goodsService) {
        yoshopGoodsServiceService.editGoodsServiceById(goodsService, storeId);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除服务与承诺")
    @PostMapping("/delete")
    public R delete(@RequestBody @Validated(ValidationGroups.Delete.class) GoodsServiceDto service) {
        yoshopGoodsServiceService.deleteGoodsServiceById(service.getServiceId(), storeId);

        return R.ok();
    }

    @ApiOperation("服务上架/下架")
    @PostMapping("/state")
    public R state(@RequestBody @Validated({ValidationGroups.Edit.class}) GoodsServiceDto goodsService) {
        boolean flag =  yoshopGoodsServiceService.updateGoodsServiceState(goodsService);
        return R.ok();
    }

}
